package com.ruoyi.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.RiverNews;
import com.ruoyi.mapper.RiverNewsMapper;
import com.ruoyi.service.IRiverNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * river_newsService业务层处理
 * 
 * @author ya
 * @date 2025-06-22
 */
@Service
public class RiverNewsServiceImpl extends ServiceImpl<RiverNewsMapper, RiverNews> implements IRiverNewsService
{
    @Autowired
    private RiverNewsMapper riverNewsMapper;

    

    /**
     * 查询river_news
     * 
     * @param newsId river_news主键
     * @return river_news
     */
    @Override
    public RiverNews selectRiverNewsByNewsId(Long newsId)
    {
        return riverNewsMapper.selectRiverNewsByNewsId(newsId);
    }

    /**
     * 查询river_news列表
     * 
     * @param riverNews river_news
     * @return river_news
     */
    @Override
    public List<RiverNews> selectRiverNewsList(RiverNews riverNews)
    {
        return riverNewsMapper.selectRiverNewsList(riverNews);
    }

    /**
     * 新增river_news
     * 
     * @param riverNews river_news
     * @return 结果
     */
    @Override
    public int insertRiverNews(RiverNews riverNews)
    {
        riverNews.setCreateTime(DateUtils.getNowDate());
        // 设置默认状态为上架（0）
        if (riverNews.getStatus() == null) {
            riverNews.setStatus(0);
        }
        return riverNewsMapper.insertRiverNews(riverNews);
    }

    @Override
    public int insertRiverNewsOwn(RiverNews riverNews) {
        // 设置默认状态为上架（0）
        if (riverNews.getStatus() == null) {
            riverNews.setStatus(0);
        }
        
        // 设置默认排序值为最大值+1
        if (riverNews.getSortOrder() == null) {
            int maxSortOrder = riverNewsMapper.selectMaxSortOrder();
            riverNews.setSortOrder(maxSortOrder + 1);
        }

        // 添加新的河道要闻
        return riverNewsMapper.insertRiverNewsOwn(riverNews);
    }

    /**
     * 修改river_news
     * 
     * @param riverNews river_news
     * @return 结果
     */
    @Override
    public int updateRiverNews(RiverNews riverNews)
    {
        //riverNews.setUpdateTime(DateUtils.getNowDate());
        return riverNewsMapper.updateRiverNews(riverNews);
    }

    @Override
    public int updateRiverNewsOwn(RiverNews riverNews) {
        return riverNewsMapper.updateRiverNewsOwn(riverNews);
    }

    /**
     * 批量删除river_news
     * 
     * @param newsIds 需要删除的river_news主键
     * @return 结果
     */
    @Override
    public int deleteRiverNewsByNewsIds(Long[] newsIds)
    {
        return riverNewsMapper.deleteRiverNewsByNewsIds(newsIds);
    }

    /**
     * 删除river_news信息
     *
     * @param newsId river_news主键
     * @return 结果
     */
    @Override
    public int deleteRiverNewsByNewsId(Long newsId)
    {
        return riverNewsMapper.deleteRiverNewsByNewsId(newsId);
    }

    /**
     * 查询未下架且最新日期的8条河道要闻
     *
     * @return 河道要闻集合
     */
    @Override
    public List<RiverNews> selectLatest8RiverNews() {
        return riverNewsMapper.selectLatest8RiverNews();
    }
    
    /**
     * 下架river_news信息
     *
     * @param newsId river_news主键
     * @return 结果
     */
    @Override
    public int offShelfRiverNewsByNewsId(Long newsId)
    {
        RiverNews riverNews = new RiverNews();
        riverNews.setNewsId(newsId);
        riverNews.setStatus(1); // 1表示隐藏
        return riverNewsMapper.updateRiverNews(riverNews);
    }

    /**
     * 上架river_news信息
     *
     * @param newsId river_news主键
     * @return 结果
     */
    @Override
    public int onShelfRiverNewsByNewsId(Long newsId)
    {
        RiverNews riverNews = new RiverNews();
        riverNews.setNewsId(newsId);
        riverNews.setStatus(0); // 0表示显示
        return riverNewsMapper.updateRiverNews(riverNews);
    }
    
    /**
     * 获取最大排序值
     * 
     * @return 最大排序值
     */
    @Override
    public int selectMaxSortOrder()
    {
        return riverNewsMapper.selectMaxSortOrder();
    }
    
    /**
     * 将排序值小于给定值的记录的排序值加1
     * 
     * @param sortOrder 排序值阈值
     */
    @Override
    public void incrementSortOrderLessThan(int sortOrder)
    {
        riverNewsMapper.incrementSortOrderLessThan(sortOrder);
    }
    
    /**
     * 查找排序值刚好小于给定值的记录
     * 
     * @param sortOrder 排序值
     * @return 记录对象
     */
    @Override
    public RiverNews selectPreviousRiverNews(int sortOrder)
    {
        return riverNewsMapper.selectPreviousRiverNews(sortOrder);
    }
    
    /**
     * 查找排序值刚好大于给定值的记录
     * 
     * @param sortOrder 排序值
     * @return 记录对象
     */
    @Override
    public RiverNews selectNextRiverNews(int sortOrder)
    {
        return riverNewsMapper.selectNextRiverNews(sortOrder);
    }
}
